package com.kgc.controller;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：周申宇 <p>
 * @Date:2021/6/16 14:24
 * <p>
 * @Decription:  请求控制器的基类，包含一些通用方法
 */
public class BaseController {
    /**
     * @Auther: 周申宇
     * <p>
     * @Date: 2021/6/16
     * <p>
     * @Description: 从request获取参数，获取不到，直接置空字符串
     */
    protected  String getParameter(HttpServletRequest request, String key){

        return request.getParameter("key") == null ? "" : request.getParameter("key");
    }

    /**
     * @Auther: 周申宇
     * <p>
     * @Date: 2021/6/16
     * <p>
     * @Description: 从request获取参数，获取不到，直接指定默认值
     */
    protected  String getParameterDefaultValue(HttpServletRequest request,String key,String defVaule){

        return request.getParameter("key") == null ? defVaule : request.getParameter("key");
    }

    /**
     * @Auther: 周申宇
     * <p>
     * @Date: 2021/6/16
     * <p>
     * @Description: 根据请求获取ip
     */
    protected String getRemoteIp(HttpServletRequest request) {
        // 获取ip
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
